﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
namespace CHUYEnPHATNHANH.admin
{
    public partial class AdminPages : System.Web.UI.MasterPage
    {
        public static string sUlLi = "";
        public static string strCss = "";
        public static string strCssLogo = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Models.LoginResult login = Models.ManagerSessionCache.GetSessionLogin();
            if (login != null)
            {
                lblHello.Text = "Welcome " + login.username + "&nbsp;&nbsp;&nbsp;";
                LoadMoudle();
            }
            else {
                string url = "";
                url = Constant.ROOT_PATH + "admin/" + Constant.loginAdmin;
                Response.Redirect(url);
            }
        }

        private void LoadMoudle() {
            List<String> listCss = Constant.GetCssAdmin();
            strCss = "";
            foreach (string m in listCss)
            {
                strCss += m;
            }
            string strClass = "";
            String strCurrent = "ht";
            try
            {
                strCurrent = Request.Params["qt"].ToString();
            }
            catch
            {
                strCurrent = "ht";
            }
            String strLi = "<li><a id =\"$id\" href=\"$href\" $class>$title</a></li>";
            string strMenu = "<li><a id=\"$id\"  $class > $title </a>$menu </li>";
            List<Module> list = Constant.GetModule();
            sUlLi = "";
            foreach (Module _module in list) {
                strClass = "";
                if (strCurrent.ToLower().Trim() == _module.ModuleName.ToLower().Trim())
                {
                    strClass = "class=\"current\"";
                }
                
                string sMenu = "<ul>";
                List<Item> items = Constant.GetItemsInModule(_module.Id);
                string url = "";
                if (items.Count > 0)
                {
                    foreach (Item _item in items) {
                        url = string.Format("{0}/{1}", _module.ModuleName, _item.ItemName);
                        
                        sMenu += strLi.Replace("$href", _item.Url + "?qt=" + _module.ModuleName + "&pro=" + _item.ItemName).Replace("$class", "").
                            Replace("$title", _item.ItemTitle).Replace("$id", _module.ModuleName + "_" + _item.Id);
                        /*
                        sMenu += strLi.Replace("$href", Constant.ROOT_PATH +  url).Replace("$class", "").
                            Replace("$title", _item.ItemTitle).Replace("$id", _module.ModuleName + "_" + _item.Id);*/
                    }
                    sMenu += "</ul>";
                    
                    sUlLi += strMenu.Replace("$id", "menu_" + _module.ModuleName).Replace("$class", strClass).Replace("$title", _module.ModuleTitle).Replace("$menu", sMenu);
                }
                else {
                    url = string.Format("{0}/{1}", _module.ModuleName, _module.ModuleName);
                    
                    sUlLi = sUlLi + strLi.Replace("$href", _module.Url + "?qt=" + _module.ModuleName + "&pro=" + _module.ModuleName).Replace("$class", strClass).
                    Replace("$title", _module.ModuleTitle).Replace("$id", "menu_" +  _module.ModuleName);
                    /*
                    sUlLi = sUlLi + strLi.Replace("$href", Constant.ROOT_PATH + url).Replace("$class", strClass).
                    Replace("$title", _module.ModuleTitle).Replace("$id", "menu_" + _module.ModuleName);
                    */
                }
            }
        }
    }
}